//
//  UITextView+QSExtension.m
//  ZengLongSeSha
//
//  Created by apple on 2017/6/23.
//  Copyright © 2017年 Song. All rights reserved.
//

#import "UITextView+QSExtension.h"
#import <objc/runtime.h>

@interface UITextView()<UITextViewDelegate>

@end

#pragma mark - 新增属性key
static NSString *placeholderViewKey = @"placeholderViewKey";
static NSString *placeholderKey = @"placeholderKey";
static NSString *placeholderColorKey = @"placeholderColorKey";
static NSString *placeholderFontKey = @"placeholderFontKey";
static NSString *limitTextLengthKey = @"limitTextLengthKey";

static NSString *shouldBeginEditBlockKey = @"shouldBeginEditBlockKey";
static NSString *didChangeBlockKey = @"didChangeBlockKey";
static NSString *endEditBlockKey = @"endEditBlockKey";
static NSString *returnKeyBlockKey = @"returnKeyBlockKey";

@implementation UITextView (QSExtension)

/**
 *  创建占位符的TextView
 */
- (void)createPlaceHilderViewWithPlaceholder:(NSString *)placeholder
{
    UITextView *placeholderView = [[UITextView alloc] init];
    placeholderView.scrollEnabled = NO;
    placeholderView.showsHorizontalScrollIndicator = NO;
    placeholderView.showsVerticalScrollIndicator = NO;
    placeholderView.userInteractionEnabled = NO;
    placeholderView.font = self.placeholderFont ? self.placeholderFont : self.font;
    placeholderView.textColor = [UIColor lightGrayColor];
    placeholderView.backgroundColor = [UIColor clearColor];
    placeholderView.text = placeholder;
    [self addSubview:placeholderView];
    self.placeholderView = placeholderView;
}

- (void)layoutSubviews
{
    if (self.placeholderView) {
        self.placeholderView.frame = self.bounds;
    }
}

#pragma mark - UITextViewDelegate
/**
 *  是否允许开始编辑
 */
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if (self.shouldBeginEditBlock) {
        return self.shouldBeginEditBlock();
    }
    return YES;
}

/**
 *  textView输入发生改变
 */
- (void)textViewDidChange:(UITextView *)textView
{
    if (self.placeholderView) {
        self.placeholderView.hidden = !kStringIsEmpty(textView.text);
    }
    
    if (textView == self) {
        if (textView.text.length > self.limitTextLength) {
            textView.text = [textView.text substringToIndex:self.limitTextLength];
        }
    }
    
    if (self.didChangeBlock) {
        self.didChangeBlock(textView.text);
    }
}

/**
 *  结束编辑
 */
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (self.endEditBlock) {
        self.endEditBlock(textView.text);
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    /**
     *  是否响应键盘的return按钮点击
     */
    if ([text isEqualToString:@"\n"]) {
        
        [textView resignFirstResponder];
        if (self.returnKeyBlock) {
            self.returnKeyBlock(textView.text);
        }
        return NO;
    }
    
    return YES;
}

#pragma mark - 占位符的TextView
- (void)setPlaceholderView:(UITextView *)placeholderView
{
    objc_setAssociatedObject(self, &placeholderViewKey, placeholderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UITextView *)placeholderView
{
    return objc_getAssociatedObject(self, &placeholderViewKey);
}

#pragma mark - 占位文字
- (void)setPlaceholder:(NSString *)placeholder
{
    self.delegate = self.delegate ? self.delegate : self;
    if (!self.placeholderView) {
        [self createPlaceHilderViewWithPlaceholder:placeholder];
    }
    self.placeholderView.text = placeholder;
    objc_setAssociatedObject(self, &placeholderKey, placeholder, OBJC_ASSOCIATION_COPY);
}

- (NSString *)placeholder
{
    return objc_getAssociatedObject(self, &placeholderKey);
}

#pragma mark - 占位文字颜色
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    self.delegate = self.delegate ? self.delegate : self;
    self.placeholderView.textColor = placeholderColor;
    objc_setAssociatedObject(self, &placeholderColorKey, placeholderColor, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIColor *)placeholderColor
{
    return objc_getAssociatedObject(self, &placeholderColorKey);
}

#pragma mark - 占位文字的字体
- (void)setPlaceholderFont:(UIFont *)placeholderFont
{
    self.delegate = self.delegate ? self.delegate : self;
    self.placeholderView.font = placeholderFont;
    objc_setAssociatedObject(self, &placeholderFontKey, placeholderFont, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIFont *)placeholderFont
{
    return objc_getAssociatedObject(self, &placeholderFontKey);
}

#pragma mark - 限制输入的长度
- (void)setLimitTextLength:(NSInteger)limitTextLength
{
    self.delegate = self.delegate ? self.delegate : self;
    objc_setAssociatedObject(self, &limitTextLengthKey, @(limitTextLength), OBJC_ASSOCIATION_COPY);
}

- (NSInteger)limitTextLength
{
    NSNumber *number = objc_getAssociatedObject(self, &limitTextLengthKey);
    return [number integerValue];
}

#pragma mark - 是否允许开始编辑回调
- (void)setShouldBeginEditBlock:(BOOL (^)(void))shouldBeginEditBlock
{
    self.delegate = self.delegate ? self.delegate : self;
    objc_setAssociatedObject(self, &shouldBeginEditBlockKey, shouldBeginEditBlock, OBJC_ASSOCIATION_COPY);
}

- (BOOL (^)(void))shouldBeginEditBlock
{
    return objc_getAssociatedObject(self, &shouldBeginEditBlockKey);
}

#pragma mark - 内容改变回调
- (void)setDidChangeBlock:(void (^)(NSString *))didChangeBlock
{
    self.delegate = self.delegate ? self.delegate : self;
    objc_setAssociatedObject(self, &didChangeBlockKey, didChangeBlock, OBJC_ASSOCIATION_COPY);
}

- (void (^)(NSString *))didChangeBlock
{
    return objc_getAssociatedObject(self, &didChangeBlockKey);
}

#pragma mark - 结束编辑回调
- (void)setEndEditBlock:(void (^)(NSString *))endEditBlock
{
    self.delegate = self.delegate ? self.delegate : self;
    objc_setAssociatedObject(self, &endEditBlockKey, endEditBlock, OBJC_ASSOCIATION_COPY);
}

- (void (^)(NSString *))endEditBlock
{
    return objc_getAssociatedObject(self, &endEditBlockKey);
}

#pragma mark - return键事件回调
- (void)setReturnKeyBlock:(void (^)(NSString *))returnKeyBlock
{
    self.delegate = self.delegate ? self.delegate : self;
    objc_setAssociatedObject(self, &returnKeyBlockKey, returnKeyBlock, OBJC_ASSOCIATION_COPY);
}

- (void (^)(NSString *))returnKeyBlock
{
    return objc_getAssociatedObject(self, &returnKeyBlockKey);
}
@end
